package com.lemon.components.tmp.download;

import java.awt.BorderLayout;
import java.awt.Desktop;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.UIManager;

import org.apache.commons.lang3.StringUtils;

public class VideoDownload {

	private JFrame frame;
	private JTextField saveLocale;
	private JTextArea textarea;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			@Override
			public void run() {
				try {
					VideoDownload window = new VideoDownload();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public VideoDownload() {
		String lookAndFeel =UIManager.getSystemLookAndFeelClassName();
		try {
			UIManager.setLookAndFeel(lookAndFeel);
		} catch (Exception e) {
			e.printStackTrace();
		}
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setTitle("腾讯新闻下载工具");
		frame.setBounds(100, 100, 583, 267);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(new BorderLayout(0, 0));
		frame.setLocationRelativeTo(null);
		
		JPanel buttonbar = new JPanel();
		frame.getContentPane().add(buttonbar, BorderLayout.SOUTH);
		buttonbar.setLayout(new FlowLayout(FlowLayout.RIGHT, 5, 5));
		
		JButton btnNewButton = new JButton("下载");
		btnNewButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				if(StringUtils.isBlank(textarea.getText())) {
					JOptionPane.showMessageDialog(null, "请输入m3u8地址", "错误提示",JOptionPane.ERROR_MESSAGE);
					return;
				}
				DownloadWindow downloadWindow = new DownloadWindow();
				downloadWindow.setLocationRelativeTo(frame);
				downloadWindow.setModal(false);
				downloadWindow.setVisible(true);
				new Thread(new Runnable() {
					@Override
					public void run() {
						VideoDownloadHandle videoDownloadHandle = new VideoDownloadHandle();
						videoDownloadHandle.downloadVideo(
								textarea.getText(),
								saveLocale.getText(),
								downloadWindow.getProgressBar(),
								downloadWindow.getProgressBar_1()
							);
						downloadWindow.dispose();
					}
				}).start();
				
			}
		});
		buttonbar.add(btnNewButton);
		
		JPanel formPanel = new JPanel();
		frame.getContentPane().add(formPanel, BorderLayout.CENTER);
		GridBagLayout gbl_formPanel = new GridBagLayout();
		gbl_formPanel.columnWidths = new int[] {90, 0, 0, 0, 5};
		gbl_formPanel.rowHeights = new int[] {0, 0, 0};
		gbl_formPanel.columnWeights = new double[]{0.0, 1.0, 0.0, 0.0, Double.MIN_VALUE};
		gbl_formPanel.rowWeights = new double[]{1.0, 0.0, Double.MIN_VALUE};
		formPanel.setLayout(gbl_formPanel);
		
		JLabel lblNewLabel = new JLabel("m3u8地址");
		lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
		GridBagConstraints gbc_lblNewLabel = new GridBagConstraints();
		gbc_lblNewLabel.insets = new Insets(0, 0, 5, 5);
		gbc_lblNewLabel.gridx = 0;
		gbc_lblNewLabel.gridy = 0;
		formPanel.add(lblNewLabel, gbc_lblNewLabel);
		
		JScrollPane scrollPane = new JScrollPane();
		GridBagConstraints gbc_scrollPane = new GridBagConstraints();
		gbc_scrollPane.gridwidth = 3;
		gbc_scrollPane.insets = new Insets(0, 0, 5, 5);
		gbc_scrollPane.fill = GridBagConstraints.BOTH;
		gbc_scrollPane.gridx = 1;
		gbc_scrollPane.gridy = 0;
		formPanel.add(scrollPane, gbc_scrollPane);
		
		textarea = new JTextArea();
		textarea.setRows(8);
		textarea.setPreferredSize(new Dimension(24, 5));
		textarea.setLineWrap(true);
		scrollPane.add(textarea);
		scrollPane.setViewportView(textarea);
		
		JLabel lblNewLabel_1 = new JLabel("本地保存地址");
		lblNewLabel_1.setHorizontalAlignment(SwingConstants.CENTER);
		GridBagConstraints gbc_lblNewLabel_1 = new GridBagConstraints();
		gbc_lblNewLabel_1.anchor = GridBagConstraints.EAST;
		gbc_lblNewLabel_1.insets = new Insets(0, 0, 0, 5);
		gbc_lblNewLabel_1.gridx = 0;
		gbc_lblNewLabel_1.gridy = 1;
		formPanel.add(lblNewLabel_1, gbc_lblNewLabel_1);
		
		saveLocale = new JTextField();
		saveLocale.setText("D:/新建文件夹");
		saveLocale.setEditable(false);
		GridBagConstraints gbc_saveLocale = new GridBagConstraints();
		gbc_saveLocale.insets = new Insets(0, 0, 0, 5);
		gbc_saveLocale.fill = GridBagConstraints.HORIZONTAL;
		gbc_saveLocale.gridx = 1;
		gbc_saveLocale.gridy = 1;
		formPanel.add(saveLocale, gbc_saveLocale);
		saveLocale.setColumns(10);
		
		JButton btnNewButton_1 = new JButton("浏览...");
		btnNewButton_1.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				JFileChooser jfileChooser = new JFileChooser(new File(saveLocale.getText()));
				jfileChooser.setFileSelectionMode(1);
				if(jfileChooser.showOpenDialog(frame) == JFileChooser.APPROVE_OPTION) {
					saveLocale.setText(jfileChooser.getSelectedFile().getAbsolutePath().replaceAll("\\\\", "/"));
				}
			}
		});
		GridBagConstraints gbc_btnNewButton_1 = new GridBagConstraints();
		gbc_btnNewButton_1.insets = new Insets(0, 0, 0, 5);
		gbc_btnNewButton_1.gridx = 2;
		gbc_btnNewButton_1.gridy = 1;
		formPanel.add(btnNewButton_1, gbc_btnNewButton_1);
		
		JButton btnNewButton_2 = new JButton("打开文件夹");
		btnNewButton_2.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				try {
					String path=saveLocale.getText();
					Desktop.getDesktop().open(new File(path));
	            } catch (Exception e1) {
	                JOptionPane.showMessageDialog(null, "打开目录失败！", "错误提示",JOptionPane.ERROR_MESSAGE);
                }
			}
		});
		GridBagConstraints gbc_btnNewButton_2 = new GridBagConstraints();
		gbc_btnNewButton_2.gridx = 3;
		gbc_btnNewButton_2.gridy = 1;
		formPanel.add(btnNewButton_2, gbc_btnNewButton_2);
	}

}
